package com.hmeter.mongodb;

import java.net.UnknownHostException;

import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;

public class MongoDbDeleteSampler extends AbstractSampler {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5390999833963494979L;
	
	private static final String HOST = "HOST";
	private static final String PORT = "PORT";
	private static final String DBNAME = "DBNAME";
	private static final String COLLECTIONNAME = "COLLECTIONNAME";
	private static final String DELETEALLDOCUMENTSFROMCOLLECTIONBOOL = "DELETEALLDOCUMENTSFROMCOLLECTIONBOOL";
	private static final String DELETEDOCUMENTSFROMCOLLECTIONBYCLAUSEBOOL = "DELETEDOCUMENTFROMCOLLECTIONBYCLAUSEBOOL";
	private static final String QUERYLIST = "QUERYLIST";
	private static final String DELIMITER = "DELIMITER";
	private static final String OPERATOR = "OPERATOR";

	@Override
	public SampleResult sample(Entry arg0) {
		SampleResult result = new SampleResult();
		
		StringBuilder requestData = new StringBuilder();
		requestData.append("Mongo DB host:port: " + this.getHost() +":"+ this.getPort() + "\n");
		requestData.append("Mongo DB name: " + this.getDbName() + "\n");
		requestData.append("Collection name: " + this.getCollectionName() + "\n");
		requestData.append("Query list: " + this.getQueryList() + "\n");
		requestData.append("Delimiter: " + this.getDelimiter() + "\n");
		requestData.append("Operator on query list: " + this.getOperator() + "\n");
		
		result.setSampleLabel(getName());
		result.setSamplerData(requestData.toString());
		result.setDataType(SampleResult.TEXT);
		
		result.sampleStart();
				
		if (this.isDeleteAllDocumentsFromCollectionBool())
		{	
			try 
			{
				MongoDbDeleteSamplerUtility mongoDelete = new MongoDbDeleteSamplerUtility(this.getHost(), Integer.parseInt(this.getPort()), this.getDbName(), this.getCollectionName());
				result.setResponseData(mongoDelete.deleteAllDocumentsFromCollection().getBytes());
				result.setSuccessful(true);
			}
			catch (UnknownHostException e1)
			{
				result.setResponseData(e1.getMessage().getBytes());
				result.setSuccessful(false);
			}
			catch (Exception e2)
			{
				result.setResponseData(e2.getMessage().getBytes());
				result.setSuccessful(false);			
			}
		}
		
		if (this.isDeleteDocumentsFromCollectionByClauseBool())
		{
			try
			{
				MongoDbDeleteSamplerUtility mongoDelete = new MongoDbDeleteSamplerUtility(this.getHost(), Integer.parseInt(this.getPort()), this.getDbName(), this.getCollectionName());
				result.setResponseData(mongoDelete.deleteDocumentFromCollectionByClause(mongoDelete.getMongoDbUtility().parseQueryInputContent(this.getQueryList(), this.getDelimiter()), this.getOperator()).getBytes());
				result.setSuccessful(true);
			}
			catch (UnknownHostException e1)
			{
				result.setResponseData(e1.getMessage().getBytes());
				result.setSuccessful(false);
			}
			catch (Exception e2)
			{
				result.setResponseData(e2.getMessage().getBytes());
				result.setSuccessful(false);			
			}
		}

		result.sampleEnd();
		return result;
	}
	
	public void setHost(String host)
	{
		setProperty(HOST, host);
	}
	
	public String getHost()
	{
		return getPropertyAsString(HOST);
	}
	
	public void setPort(String port)
	{
		setProperty(PORT, port);
	}
	
	public String getPort()
	{
		return getPropertyAsString(PORT);
	}
	
	public void setDbName(String dbName)
	{
		setProperty(DBNAME, dbName);
	}
	
	public String getDbName()
	{
		return getPropertyAsString(DBNAME);
	}
	
	public void setCollectionName(String collectionName)
	{
		setProperty(COLLECTIONNAME, collectionName);
	}
	
	public String getCollectionName()
	{
		return getPropertyAsString(COLLECTIONNAME);
	}
	
	public void setQueryList(String queryList)
	{
		setProperty(QUERYLIST, queryList);
	}
	
	public String getQueryList()
	{
		return getPropertyAsString(QUERYLIST);
	}
	
	public void setDelimiter(String delimiter)
	{
		setProperty(DELIMITER, delimiter);
	}
	
	public String getDelimiter()
	{
		return getPropertyAsString(DELIMITER);
	}
	
	public void setOperator(String operator)
	{
		setProperty(OPERATOR, operator);
	}
	
	public String getOperator()
	{
		return getPropertyAsString(OPERATOR);
	}
	
	public boolean isDeleteAllDocumentsFromCollectionBool()
	{
		return getPropertyAsBoolean(DELETEALLDOCUMENTSFROMCOLLECTIONBOOL);
	}
	
	public void setDeleteAllDocumentsFromCollectionBool(boolean deleteAllDocumentsFromCollection)
	{
		setProperty(DELETEALLDOCUMENTSFROMCOLLECTIONBOOL, deleteAllDocumentsFromCollection);
	}
	
	public boolean isDeleteDocumentsFromCollectionByClauseBool()
	{
		return getPropertyAsBoolean(DELETEDOCUMENTSFROMCOLLECTIONBYCLAUSEBOOL);
	}
	
	public void setDeleteDocumentsFromCollectionByClauseBool(boolean deleteDocumentsFromCollectionByClause)
	{
		setProperty(DELETEDOCUMENTSFROMCOLLECTIONBYCLAUSEBOOL, deleteDocumentsFromCollectionByClause);
	}

}
